#include "stdafx.h"
#include "BasicHttpDownLoadMgr.h"
#include "BasicHttpDownLoadPackage.h"

namespace utility
{
	BasicHttpDownLoadMgr gMgr;
	/*
	 *	desc:http down load manager
	 *	author:xxm
	 */
	BasicHttpDownLoadMgr::BasicHttpDownLoadMgr()
	{
		m_nCount = 0;
	}

	BasicHttpDownLoadMgr::~BasicHttpDownLoadMgr()
	{
	}

	void BasicHttpDownLoadMgr::HttpInit()
	{
		if (gMgr.m_nCount == 0)
		InitializeCriticalSection(&gMgr.m_section);
		gMgr.m_nCount++;
	}

	void BasicHttpDownLoadMgr::HttpClose()
	{
		if (gMgr.m_nCount > 0)
		{
			gMgr.m_nCount--;
			if (gMgr.m_nCount == 0)
			{
				if (gMgr.m_vPackage.size() == 0)
					DeleteCriticalSection(&gMgr.m_section);
				else
				{
					for(int i=0; i<gMgr.m_vPackage.size();i++)
					{
						gMgr.m_vPackage[i]->StopThread();
					}
					DeleteCriticalSection(&gMgr.m_section);
				}
			}
		}
	}

	HANDLE BasicHttpDownLoadMgr::HttpDownLoad(const TCHAR* szUrl,const TCHAR* szLocal)
	{
		BasicHttpDownLoadPackage* pPack;
		EnterSection();

		//Check Has Exist
		for(vector<BasicHttpDownLoadPackage*>::iterator it = gMgr.m_vPackage.begin();
			it != gMgr.m_vPackage.end();it++)
		{
			if (::_tcsicmp/*wcsicmp*/((*it)->m_strUrl.c_str(),szUrl) == 0
				&& ::_tcsicmp((*it)->m_strLocal.c_str(),szLocal) == 0)
			{
				pPack = *it;
				pPack->AddRef();
				LeaveSection();
				return pPack;
			}
		}

		pPack = new BasicHttpDownLoadPackage();
		if (pPack)
		{
			pPack->m_strUrl = szUrl;
			pPack->m_strLocal = szLocal;
			gMgr.m_vPackage.push_back(pPack);
			pPack->StartThread();
		}
		LeaveSection();
		return pPack;
	}

	int BasicHttpDownLoadMgr::HttpGetTaskInfo(HANDLE hTask,HTTP_DOWNLOAD_INFO* pInfo)
	{
		memset(pInfo,0, sizeof(HTTP_DOWNLOAD_INFO));
		BasicHttpDownLoadPackage* pPack = (BasicHttpDownLoadPackage*)hTask;
		if (pPack)
		{
			pInfo->nHasLoad = pPack->m_nHasLoad;
			pInfo->nLastCode = pPack->m_nLastCode;
			pInfo->nPercent = pPack->m_nTotal? (pPack->m_nHasLoad*100 / pPack->m_nTotal):0; 
			pInfo->nStatus = pPack->m_nStatus;
			pInfo->nTotal = pPack->m_nTotal;
		}
		return 0;
	}

	int BasicHttpDownLoadMgr::HttpGetTaskStatus(HANDLE hTask)
	{
		BasicHttpDownLoadPackage* pPack = (BasicHttpDownLoadPackage*)hTask;
		if (pPack)
			return pPack->m_nStatus;
		return 0;
	}

	void BasicHttpDownLoadMgr::HttpCancelTask(HANDLE hTask)
	{
		BasicHttpDownLoadPackage* pPack = (BasicHttpDownLoadPackage*)hTask;
		if (pPack)
			pPack->StopThread();
	}

	void BasicHttpDownLoadMgr::HttpCloseTask(HANDLE hTask)
	{
		HttpCancelTask(hTask);
		EnterSection();
		BasicHttpDownLoadPackage* pPack = (BasicHttpDownLoadPackage*)hTask;
		if (pPack->DecRef())
		{
			for(vector<BasicHttpDownLoadPackage*>::iterator it = gMgr.m_vPackage.begin();
				it != gMgr.m_vPackage.end();it++)
			{
				if (*it == pPack)
				{
					gMgr.m_vPackage.erase(it);
					break;
				}
			}
		}
		LeaveSection();
	}

	void BasicHttpDownLoadMgr::EnterSection()
	{
		EnterCriticalSection(&gMgr.m_section);
	}

	void BasicHttpDownLoadMgr::LeaveSection()
	{
		LeaveCriticalSection(&gMgr.m_section);
	}

	bool BasicHttpDownLoadMgr::HttpDownLoad(LPHTTP_DOWNHELP_INFO pDownHelpInfo)
	{
		BasicHttpDownLoadPackage httpDownLoadPackage;
		httpDownLoadPackage.m_strUrl = pDownHelpInfo->pUrl;
		httpDownLoadPackage.m_strLocal = pDownHelpInfo->pLocal;
		bool result = httpDownLoadPackage.ProcessHttpDownLoad();
		pDownHelpInfo->nStatus = result ? 2:3;
		return result;
	}
}